package com.ophiux.utils.parser.map;

import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import com.ophiux.utils.exception.OphiuxApiException;
import com.ophiux.utils.parser.EntityConverter;
import com.ophiux.utils.parser.IConvert;

public class MapConverter implements IConvert {

	@Override
	public <T> T toEntity(Class<T> clzz, String dataSource) throws OphiuxApiException {
		return null;
	}

	@Override
	public <T> String entityToString(T obj) throws OphiuxApiException {
		return null;
	}

	@Override
	public <T> SortedMap entityToMap(T obj) throws OphiuxApiException {

		return parseEntityToMap(obj);
	}

	public static <T> SortedMap parseEntityToMap(T obj) throws OphiuxApiException {
		MapWriter writer = new MapWriter() {
			@Override
			public <T> SortedMap writerFromArray(List<T> valueList) throws OphiuxApiException {
				SortedMap<String, Object> listMap = new TreeMap<String, Object>();
				if (valueList != null && valueList.size() != 0) {
					for (T obj : valueList) {
						String objStr = "";
						SortedMap<String, Object> objMap = parseEntityToMap(obj);
						listMap.putAll(objMap);
					}
				}
				return listMap;
			}

			@Override
			public SortedMap writerFromObject(Object obj) throws OphiuxApiException {
				return parseEntityToMap(obj);
			}

		};
		return EntityConverter.writer2Map(obj, writer);
	}
}
